<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>axios封装 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="首先安装axios1npm install --save axios    新建一个http.js和一个api.js文件，http.js文件用来封装我们的axios，api.js用来统一管理我们的接口。   配置axios创建一个http.js文件，这里可以存放axios的配置和请求、响应拦截器1234567891011121314151617181920212223242526272829303">
<meta property="og:type" content="article">
<meta property="og:title" content="axios封装">
<meta property="og:url" content="https://mourning-soul.gitee.io/hexo/2021/01/31/axios%E5%B0%81%E8%A3%85/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="首先安装axios1npm install --save axios    新建一个http.js和一个api.js文件，http.js文件用来封装我们的axios，api.js用来统一管理我们的接口。   配置axios创建一个http.js文件，这里可以存放axios的配置和请求、响应拦截器1234567891011121314151617181920212223242526272829303">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-30T16:00:00.000Z">
<meta property="article:modified_time" content="2021-01-31T05:29:04.050Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="-blocker">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/hexo/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/hexo/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/hexo/css/style.css">

  
    
<link rel="stylesheet" href="/hexo/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo/">Home</a>
        
          <a class="main-nav-link" href="/hexo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://mourning-soul.gitee.io/hexo"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-axios封装" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/hexo/2021/01/31/axios%E5%B0%81%E8%A3%85/" class="article-date">
  <time class="dt-published" datetime="2021-01-30T16:00:00.000Z" itemprop="datePublished">2021-01-31</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      axios封装
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="首先安装axios"><a href="#首先安装axios" class="headerlink" title="首先安装axios"></a>首先安装axios</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --save axios</span><br></pre></td></tr></table></figure>
<hr>
<br>

<h3 id="新建一个http-js和一个api-js文件，http-js文件用来封装我们的axios，api-js用来统一管理我们的接口。"><a href="#新建一个http-js和一个api-js文件，http-js文件用来封装我们的axios，api-js用来统一管理我们的接口。" class="headerlink" title="新建一个http.js和一个api.js文件，http.js文件用来封装我们的axios，api.js用来统一管理我们的接口。"></a>新建一个http.js和一个api.js文件，http.js文件用来封装我们的axios，api.js用来统一管理我们的接口。</h3><hr>
<br>

<h2 id="配置axios"><a href="#配置axios" class="headerlink" title="配置axios"></a>配置axios</h2><h3 id="创建一个http-js文件，这里可以存放axios的配置和请求、响应拦截器"><a href="#创建一个http-js文件，这里可以存放axios的配置和请求、响应拦截器" class="headerlink" title="创建一个http.js文件，这里可以存放axios的配置和请求、响应拦截器"></a>创建一个http.js文件，这里可以存放axios的配置和请求、响应拦截器</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 配置axios基本的属性</span><br><span class="line"></span><br><span class="line">const axios &#x3D; require(&#39;axios&#39;)</span><br><span class="line"></span><br><span class="line">axios.defaults.baseURL &#x3D; &#39;http:&#x2F;&#x2F;127.0.0.1:8000&#39; &#x2F;&#x2F; 请求的接口主域名</span><br><span class="line">axios.defaults.timeout &#x3D; 10000 &#x2F;&#x2F; 请求超时的设置 10s</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 请求拦截器(在请求前会自动去做的操作)，如果登陆了，那么自动带token发送</span><br><span class="line">axios.interceptors.request.use(config &#x3D;&gt; &#123;</span><br><span class="line">  let token &#x3D; localStorage.getItem(&#39;token&#39;);</span><br><span class="line">  if (token) &#123;</span><br><span class="line">    axios.defaults.headers[&#39;Authorization&#39;] &#x3D; token &#x2F;&#x2F; 头部设置</span><br><span class="line">  &#125;</span><br><span class="line">  return config;</span><br><span class="line">&#125;, error &#x3D;&gt; &#123;</span><br><span class="line">  console.log(error)</span><br><span class="line">  return Promise.reject(error);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 响应拦截器(在响应时自动会做的操作)，比如状态码返回为401时，要进入login界面</span><br><span class="line"> axios.interceptors.response.use(response &#x3D;&gt; &#123;</span><br><span class="line">   &#x2F;&#x2F; 200</span><br><span class="line">   return response.data;</span><br><span class="line"> &#125;, error &#x3D;&gt; &#123;</span><br><span class="line">   return error</span><br><span class="line"> &#125;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#x2F;**</span><br><span class="line"> * post方法，对应post请求</span><br><span class="line"> * @param &#123;String&#125; url [请求的url地址]</span><br><span class="line"> * @param &#123;Object&#125; params [请求时携带的参数]</span><br><span class="line"> * @param &#123;Object&#125; headers [请求时的头部]</span><br><span class="line"> **&#x2F;</span><br><span class="line">export function get(url, params, headers) &#123;</span><br><span class="line">  return new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">    axios.get(url, &#123;params, headers&#125;).then(res &#x3D;&gt; &#123;</span><br><span class="line">      resolve(res)</span><br><span class="line">    &#125;).catch(err &#x3D;&gt; &#123;</span><br><span class="line">      reject(err)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export function post(url, params, headers) &#123;</span><br><span class="line">  return new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">    axios.post(url, params, headers).then((res) &#x3D;&gt; &#123;</span><br><span class="line">      resolve(res)</span><br><span class="line">    &#125;).catch((err) &#x3D;&gt; &#123;</span><br><span class="line">      &#x2F;&#x2F; debugger</span><br><span class="line">      reject(err)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">export function put(url, params, headers) &#123;</span><br><span class="line">  return new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">    axios.put(url, params, headers).then((res) &#x3D;&gt; &#123;</span><br><span class="line">      resolve(res)</span><br><span class="line">    &#125;).catch((err) &#x3D;&gt; &#123;</span><br><span class="line">      &#x2F;&#x2F; debugger</span><br><span class="line">      reject(err)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export function del(url, params, headers) &#123;</span><br><span class="line">  return new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">    axios.delete(url, &#123;data: params, headers&#125;).then((res) &#x3D;&gt; &#123;</span><br><span class="line">      resolve(res)</span><br><span class="line">    &#125;).catch((err) &#x3D;&gt; &#123;</span><br><span class="line">      &#x2F;&#x2F; debugger</span><br><span class="line">      reject(err)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export default axios;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li>注意：<h3 id="我们通过定义一个（post，get，put，delete）函数，函数有两个参数，第一个参数表示我们要请求的url地址，第二个参数是我们要携带的请求参数。函数返回一个promise对象，当axios其请求成功时resolve服务器返回-值，请求失败时reject错误值。最后通过export抛出函数。"><a href="#我们通过定义一个（post，get，put，delete）函数，函数有两个参数，第一个参数表示我们要请求的url地址，第二个参数是我们要携带的请求参数。函数返回一个promise对象，当axios其请求成功时resolve服务器返回-值，请求失败时reject错误值。最后通过export抛出函数。" class="headerlink" title="我们通过定义一个（post，get，put，delete）函数，函数有两个参数，第一个参数表示我们要请求的url地址，第二个参数是我们要携带的请求参数。函数返回一个promise对象，当axios其请求成功时resolve服务器返回 值，请求失败时reject错误值。最后通过export抛出函数。"></a>我们通过定义一个（post，get，put，delete）函数，函数有两个参数，第一个参数表示我们要请求的url地址，第二个参数是我们要携带的请求参数。函数返回一个promise对象，当axios其请求成功时resolve服务器返回 值，请求失败时reject错误值。最后通过export抛出函数。</h3></li>
</ul>
<hr>
<br>

<h2 id="封装请求"><a href="#封装请求" class="headerlink" title="封装请求"></a>封装请求</h2><h3 id="创建一个api-js-文件，存放具体请求"><a href="#创建一个api-js-文件，存放具体请求" class="headerlink" title="创建一个api.js 文件，存放具体请求"></a>创建一个api.js 文件，存放具体请求</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">import &#123;get, post, put, del&#125; from &#39;.&#x2F;index&#39;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;get方法</span><br><span class="line">export const 调用名 &#x3D; parameter &#x3D;&gt; &#123;</span><br><span class="line">  return get(</span><br><span class="line">    &#39;路由地址&#39;,</span><br><span class="line">    parameter,</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; post方法</span><br><span class="line">export const 调用名 &#x3D; parameter &#x3D;&gt; &#123;</span><br><span class="line">  return post(</span><br><span class="line">    &#39;路由地址&#39;,</span><br><span class="line">    parameter,</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; put方法</span><br><span class="line">export const 调用名 &#x3D; parameter &#x3D;&gt; &#123;</span><br><span class="line">  return put(</span><br><span class="line">    &#39;路由地址&#39;,</span><br><span class="line">    parameter,</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; delete方法</span><br><span class="line">export const 调用名 &#x3D; parameter &#x3D;&gt; &#123;</span><br><span class="line">  return del(</span><br><span class="line">    &#39;路由地址&#39;,</span><br><span class="line">    parameter,</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="在vue组件中使用"><a href="#在vue组件中使用" class="headerlink" title="在vue组件中使用"></a>在vue组件中使用</h2><h3 id="创建一个-vue文件"><a href="#创建一个-vue文件" class="headerlink" title="创建一个.vue文件"></a>创建一个.vue文件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import &#123;调用名&#125; from &#39;@&#x2F;api.js&#39;;</span><br><span class="line">export default&#123;</span><br><span class="line">    created()&#123;</span><br><span class="line">        调用名()</span><br><span class="line">        .then(res&#x3D;&gt;&#123;</span><br><span class="line">            console.log(res)</span><br><span class="line">        &#125;)</span><br><span class="line">        .catch(err&#x3D;&gt;&#123;</span><br><span class="line">            console.log(err)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://mourning-soul.gitee.io/hexo/2021/01/31/axios%E5%B0%81%E8%A3%85/" data-id="ckkt9opfi0000i4tw8gka1f7x" data-title="axios封装" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          redis发布者订阅者
        
      </div>
    </a>
  
  
    <a href="/hexo/2021/01/30/celery%E5%BC%82%E6%AD%A5%E6%A1%86%E6%9E%B6(5.0)/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">celery异步框架5.0</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/hexo/tags/blocker/" style="font-size: 10px;">-blocker</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/02/">February 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">January 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo/2021/02/05/%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/">视频直播</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/">无限极分类自关联递归</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/03/webshell/">webshell</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/">redis键空间事件</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">redis发布者订阅者</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/hexo/js/jquery-3.4.1.min.js"></script>



  
<script src="/hexo/fancybox/jquery.fancybox.min.js"></script>




<script src="/hexo/js/script.js"></script>





  </div>
</body>
</html>